var productClass = requestData(path + "/repository/product-class");
var storagePosition = requestData(path + "/repository/storagePosition");
$(function(){
	
	//将查询到的供应商分类打包成map格式
	var productClassMap = {};
	var productClassOption = [];
	
	if(productClass != -1){
		for(var i = 0;i < productClass.length;i++){
			productClassMap[productClass[i].productClassId] = productClass[i].productClassName;
		}
		for(key in productClass){
			productClassOption.push({
				key: productClass[key].productClassId,
				value: productClass[key].productClassName
			});
		}
	}
	
	var data = {
			url:path+'/repository/goods-list',
			columns: [{
				field: 'checked',
				checkbox: true,
			},{
				field: 'goodsListProductId',
				visible: false
			},{
				field: 'goodsListProductName',
				title: '货物名称'
			},{
				field: 'goodsListProductAmount',
				title: '货物数量',
				sortable: true
				
			},{
				field: 'goodsListProductClass',
				title: '货物分类',
				formatter: function (value, row, index) {
					
					if(typeof(productClassMap[value]) == 'undefined'){
						return "该货物分类被删除了!";
					}else {
						return productClassMap[value];
					}
				}
			},{
				field: 'goodsListProductUnit',
				title: '货物单位'
			},{
				field: 'goodsListInStockTime',
				title: '入库时间'
			},{
				field: 'goodsListNumberState',
				title: '库存状态',
				formatter: function (value, row, index) {
					
					var currentAmount = row.goodsListProductAmount;
					var upLimit = row.goodsListUpLimit;
					var lowLimit = row.goodsListLowLimit;
					
					if(upLimit <= 0 && lowLimit <= 0){
						return "<span class='layui-badge layui-bg-cyan'>未设置上下限</span>";
					}else if(currentAmount < lowLimit) {
						return '<span class="layui-badge">库存不足</span>';
					}else if(currentAmount > lowLimit && currentAmount < upLimit){
						return '<span class="layui-badge layui-bg-orange">库存紧张</span>';
					}else{
						return '<span class="layui-badge layui-bg-green">库存充足</span>'
					}
				}
			},{
				field: 'goodsListStorageId',
				title: '所属货架',
				formatter: function (value, row, index) {
					return '<input type="button" value="查看货架" class="layui-btn layui-btn-sm" onclick="queryStorage(\''+value+'\')">';
				}
			},{
				field: 'goodsListInFlag',
				title: '对应入库单',
				formatter: function (value, row, index) {
					
					if (value == "N"){
						return '<input type="button" onclick="javascript:void(0)"' +
						'value="没有对应入库单" class="layui-btn layui-btn-sm layui-btn-disabled">'
					}
					
					return '<input type="button" value="查看对应入库单"'+
					'class="layui-btn layui-btn-sm layui-btn-normal" onclick="queryInStockOrder('+row.goodsListInStockId+')">'
				}
			},{
				field: 'goodsListOperatLog',
				title: '货物操作日志'
			}],	
			method: 'GET',//请求方式（*）
			contentType : "application/x-www-form-urlencoded;charset='UTF-8'",
			sortable: true,
			sortOrder: "asc",
			sidePagination: "server",
			search: false,
			exportOptions:{  
			   fileName: '货物清单列表'+exportDate.toLocaleString(),
			   worksheetName: 'sheet1',//表格工作区名称  
			   tableName: '货物清单列表',
				           ignoreColumn: [0,1],
						   unaffectedColumn:0
			}
	}
	
	var param = [{text:"入库时间",name:"dateScope",type:"text",verify:""},{text:"货物名称",name:"goodsListProductName",type:"text",verify:""},{text:"货物单位",name:"goodsListProductUnit",type:"text",verify:""},{text:"货物分类",name:"goodsListProductClass",type:"select",option:productClassOption}];
	
	//渲染表格
	renderTable(cTable,data);
	
	createQueryPanel(param);
	
	//条件查询 日期组建渲染
	laydate.render({
		 elem:  "[name='dateScope']"
		 ,type: 'datetime'
		 ,range: true
	});

	//渲染组建
	form.render();
})

//添加按钮
$("#add_btn").click(function(){
	openOperateWindow({
		title: "入库单添加",
		url: "/admin/enterpriseManagement/repositoryManagement/goodsList/goods-list-add.jsp"
	})
})

//删除按钮
$("#remove_btn").click(function(){
	/*fixedAlert("禁止删除货物!请到货物盘点中进行操作!",0);*/
	requestRemoveData("goodsListProductId","/repository/goods-list");
})

//修改按钮
$("#modify_btn").click(function(){
	openOperateWindow({
		title: "入库单修改",
		isUpdate: true,
		url: "/admin/enterpriseManagement/repositoryManagement/goodsList/goods-list-update.jsp"
	})
})

/**
 * 查看入库单
 * @param orderId
 * 			入库单id
 */
function queryInStockOrder(inStockId){
	$.get(path+"/repository/stock-in", { stockInId : inStockId},
       function(data){
			layer.open({
				  type: 1,
				  title: '入库单信息',
				  content: '<blockquote class="layui-elem-quote title">入库单信息</blockquote><table class="layui-table magt0"><colgroup><col width="150"></colgroup><tbody><tr><td>数据录入时间</td><td>'+data[0].stockInInputTime+'</td></tr><tr><td>入库单号</td><td>'+data[0].stockInOrderNumber+'</td></tr><tr><td>执行人员</td><td>'+data[0].stockInExecutor+'</td></tr><tr><td>入库状态</td><td>'+data[0].stockInStatus+'</td></tr><tr><td>备注</td><td>'+data[0].stockInRemark+'</td></tr></tbody></table>',
				  success: function(layero, index) {
			        layer.iframeAuto(index);
			      }
			});
	   }
    );
}

/**
 * 查看货架
 * @param storageId
 * 			货架id
 */
function queryStorage(storageId){
	$.get(path+"/repository/storagePosition", {stgPosId : storageId,statusFlag : "T"},
       function(data){
			layer.open({
			  type: 1,
			  title: '对应货架信息',
			  content: '<blockquote class="layui-elem-quote title">货架信息</blockquote><table class="layui-table magt0"><colgroup><col width="150" /></colgroup><tbody></tr>	<tr><td>性质</td><td>'+data[0].stgPosNature+'</td></tr><tr><td>层数</td><td>'+data[0].stgPosLayer+'</td></tr><tr><td>长度</td><td>'+data[0].stgPosLength+'</td></tr><tr><td>宽度</td><td>'+data[0].stgPosWidth+'</td></tr>	<tr><td>状态</td><td>'+data[0].stgPosStatus+'</td></tr><tr><td>备注</td><td>'+data[0].stgPosRemark+'</td></tr><tr><td>位置编号</td><td>'+data[0].stgPosMark+'</td></tr></tbody></table>',
			  success: function(layero, index) {
		        layer.iframeAuto(index);
		      }
			});
	   }
    );
	
}
